#!/bin/sh -f
# $Id: rcopy.sh 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 
# $Author: pgurumur $
# $Revision: 7557b5d4d82b $

PROG=`basename $0`
SOURCE=
DEST=
uid=`id -u`
RSYNC=`which rsync`
CHOWN=`which chown`
CHMOD=`which chmod`
MKDIR=`which mkdir`
STAT=`which stat`
RM=`which rm`
SED=`which sed`
FIND=`which find`
HOST=`hostname`
UNAME=`uname`
VERBOSE=0
ECHO=
COL_N="\033[0m"
COL_C="\033[36m"
COL_Y="\033[33m"
COL_M="\033[35m"

if [ ${UNAME} = "Linux" ]; then
   ECHO="echo -e"
elif [ ${UNAME} = "Darwin" ]; then
   ECHO="echo -e"
elif [ ${UNAME} = "OpenBSD" ]; then
   ECHO="echo"
fi

usage () {
   ${ECHO} "${COL_M}copy files from source to destination using rsync${COL_N}"
   echo "${PROG} options"
   echo "options: "
   echo "   -s [ source directory ]"
   echo "   -d [ destination directory ]"
   echo "   -h [ print this help ]"
   echo "   -v [ make this program more verbose ]"
   exit 1
}

err () {
   ${ECHO} "${COL_Y}${PROG}:" $* "${COL_N}"
   exit 1
}


if [ $# -lt 1 ]; then
   usage
else
   while getopts s:d:vh arg; do
      case ${arg} in
         s)
            SOURCE=${OPTARG}
            ;;

         d)
            DEST=${OPTARG}
            ;;

         h)
            usage
            ;;

         v)
            VERBOSE=1
            ;;

         *)
            usage
            ;;
      esac
   done

   OPTIONS="-putzar" 
   test ${VERBOSE} -eq 1 && OPTIONS="${OPTIONS} -v"

   test ${uid} -eq 0 || err "Need to be root user!"
   test -d ${SOURCE} || err "${SOURCE} does not exist"
   test -d ${DEST} || echo "${DEST} does not exist, rsync will create it!"
   SOURCE=`echo ${SOURCE} | ${SED} -e 's/\/$//g'`
   DEST=`echo ${DEST} | ${SED} -e 's/\/$//g'`
   TEMP=`basename ${SOURCE}`
   NEWDEST="${DEST}/$TEMP/"
   test -d ${NEWDEST} && ${RM} -rf ${NEWDEST}

   DIRUID=
   if [ ${UNAME} = "Linux" ]; then
      DIRUID=`${STAT} -c "%u" ${DEST}`
   elif [ ${UNAME} = "Darwin" ]; then
      DIRUID=`${STAT} -f "%u" ${DEST}`
   elif [ ${UNAME} = "OpenBSD" ]; then
      DIRUID=`${STAT} -f "%u" ${DEST}`
   fi

   test ${DIRUID} -eq ${uid} || err "$DEST isn't owned by user with uid: ${uid}"
   ${RSYNC} ${OPTIONS} --stats ${SOURCE} ${DEST}/
   ${CHOWN} -R root:root ${DEST}/
   ${FIND} ${NEWDEST}/ -type f -name "*.pyc" -exec rm -f {} \;
   ${FIND} ${NEWDEST}/ -type d -perm 0700 -exec chmod 0755 {} \;
   ${FIND} ${NEWDEST}/ -type f -perm 0600 -exec chmod 0644 {} \;
   test -d ${NEWDEST}/var/tmp || ${MKDIR} -p ${NEWDEST}/var/tmp
   if [ ${HOST} = "sirius" ]; then
      ${CHOWN} wwwrun:root ${NEWDEST}/var/tmp
      ${CHMOD} 1777 ${NEWDEST}/var/tmp
   else
      ${CHOWN} root:daemon ${NEWDEST}/var/tmp
      ${CHMOD} 1777 ${NEWDEST}/var/tmp
   fi

   ${ECHO} "${COL_C}${PROG}: ${SOURCE} copied into directory ${DEST}${COL_N}"
fi
